<?php

namespace App\Models\Admin\Stats\Platform\Settlement;

use App\Models\Admin\Model;
use App\Models\Game\Platform\Org;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class Day extends Model
{

    /**
     * 数据表名称
     *
     * @var string
     */
    protected $table = 'stats_platform_settlement_days';

    /**
     * 不可批量赋值的字段
     *
     * @var array
     */
    protected $guarded = [];

    /**
     * 进行类型转换的字段
     *
     * @var array
     */
    protected $casts = [
        'usd' => 'array',
    ];

    /**
     * 获得平台信息
     *
     * @return BelongsTo
     */
    public function org(): BelongsTo
    {
        return $this->belongsTo(Org::class, 'plat_code', 'code')->withTrashed();
    }

    /**
     * 获取去重统计字段列表
     *
     * @param int $timeStart
     * @param int $timeEnd
     * @param array $fields
     * @return \Illuminate\Support\Collection
     */
    protected function getStatsDistinct(int $timeStart, int $timeEnd, array $fields)
    {
        return $this->newQuery()->getQuery()->select($fields)
            ->where('timed', '>=', $timeStart)
            ->where('timed', '<', $timeEnd)
            ->distinct()
            ->get();
    }
}
